if not HST then return end

local L = HST.L

-- Used to check global cooldown
HST.SerpentStingIndex = 1

-- Returns 1 if currently is global cooldown time
function HST:CheckGCD()
    if GetSpellName(self.SerpentStingIndex, "BOOKTYPE_SPELL") ~= HST.L["Serpent Sting"] then
        self.SerpentStingIndex = self:GetSpellIndex(HST.L["Serpent Sting"])
    end
    if not self.SerpentStingIndex then return false end
    if GetSpellCooldown(self.SerpentStingIndex,"BOOKTYPE_SPELL") == 0 then return nil else return 1 end
end

function HST:GetSpellIndex(SpellName)
	local spell, rank = GetSpellInfo(SpellName, BOOKTYPE_SPELL)
	return GetSpellIndex(spell, rank)
end

function HST:GetSpellIcon(SpellName)
	local id, icon = self:GetSpellIndex(SpellName), nil
	if id then _,_,icon = GetSpellInfo(id, BOOKTYPE_SPELL) end
	return icon
end

function HST:CastSpell(spellId, spellbookTabNum)
	if self:CheckGCD() or self.castblock then return end
	self.hooks.CastSpell(spellId, spellbookTabNum)
	local _,cd = GetSpellCooldown(spellId,spellbookTabNum)
	if self.casting == true or cd == 6 then
		return
	end
	local spellName, rank = GetSpellName(spellId, spellbookTabNum)
	_,_,rank = string.find(rank,"(%d+)")
	if self.ShotSpells[spellName] and not self.castblock then
		self:StartCast(spellName, rank)
	end
end

function HST:CastSpellByName(spellName, onSelf)
	if self:CheckGCD() or self.castblock then return end
	self.hooks.CastSpellByName(spellName, onSelf)

	for i=1,120 do
		if IsCurrentAction(i) then
			break
		end
		if i == 120 then return end
	end

	local _,_,rank = string.find(spellName,"(%d+)")
	local _, _,spellName = string.find(spellName, "^([^%(]+)")
	if not rank then
		local i = 1
		while GetSpellName(i, BOOKTYPE_SPELL) do
			local s, r = GetSpellName(i, BOOKTYPE_SPELL)
			if s == spellName then
				rank = r
			end
			i = i+1
		end
		if rank then
			_,_,rank = string.find(rank,"(%d+)")
		end
	end
	if self.ShotSpells[spellName] and not self.castblock then
		SpellStartTime=GetTime();
		self:StartCast(spellName, rank)
	end
end

function HST:UseAction(slot, checkCursor, onSelf)
	if self:CheckGCD() or self.castblock then return end
	
	self.hooks.UseAction(slot, checkCursor, onSelf)

	if GetActionText(slot) or not IsCurrentAction(slot) then return end

	self.ScanTip:ClearLines()
	self.ScanTip:SetAction(slot)
	local spellName = HSTScanTipTextLeft1:GetText()
	local rank = HSTScanTipTextRight1:GetText()
	if rank then
		_,_,rank = string.find(rank,"(%d+)")
	end
	if not rank then
		rank = 1
	end
	if self.ShotSpells[spellName] and not self.castblock then
		self:StartCast(spellName, rank)
	end
end

-- Exclude spells that are not supported before calling this function
function HST:StartCast(SpellName, Rank)
	local enabled, casttime = self:GetSpellCastBarTime(SpellName)
	if not casttime then 
		self.casting = true 
		return 
	end

	if enabled then 
		self:ShowCastBar(SpellName, self:GetSpellIcon(SpellName), casttime, GetTime()) 
	end

	self:ScheduleEvent("DELAY_SHOT", self.DELAY_SHOT, 0.2, self)
end

function HST:GetSpellCastBarTime(SpellName)
	local enabled, casttime = nil, nil
	if SpellName == L["Aimed Shot"] then
		enabled = self.db.profile.aimed
		casttime = self:GetAimedShotCastTime()
	elseif SpellName == L["Multi-Shot"] then
		enabled = self.db.profile.multi
		casttime = self:GetMultiShotCastTime() 
    elseif SpellName == L["Trueshot"] then
		enabled = self.db.profile.trueshot
		casttime = self:GetSteadyShotCastTime()     
	elseif SpellName == L["Tranquilizing Shot"] and self.db.profile.tranq then
		self:ScheduleEvent("HST_TRANQ", self.Announce, 0.2, self, 
			string.gsub(self.db.profile.tranqmsg,"%%t",UnitName("target")))
	end
	return enabled, casttime
end